document.write('<script type="text/javascript" src="./js/sys_config.js"></script>');

$(function() {

    showTable();

});

function emptyInputField(){
    $("#semester").val('');
    $("#week").val('');
    $("#recordDate").val('');
    $("#stuNumber").val('');
    $("#stuName").val('');
    $("#stuClass").val('');
    $("#classType").val('');
    $("#date").val('');
    $("#lesson").val('');
    $("#teacher").val('');
    $("#classroom").val('');
    $("#lessonTime").val('');
    $("#remark").val('');
}
function showTable(){
    $.ajax({
        type:'GET',
        url:restAPI_url+'absenteeism?sort=_id',
        success:function(data){
             $("tbody").empty();
             for(var i=0;i<data.length;i++){
                var $html = '<tr id="appendData">';
                $html = $html +'<td class="hidden">'+data[i]._id+'</td>';
                $html = $html +'<td >'+data[i].semester+'</td>';
                $html = $html +'<td >'+data[i].week+'</td>';
                $html = $html +'<td >'+data[i].recordDate+'</td>';
                $html = $html +'<td >'+data[i].stuNumber+'</td>';
                $html = $html +'<td >'+data[i].stuName+'</td>';
                $html = $html +'<td >'+data[i].stuClass+'</td>';
                $html = $html +'<td >'+data[i].classType+'</td>';
                $html = $html +'<td >'+data[i].date+'</td>';
                $html = $html +'<td >'+data[i].lesson+'</td>';
                $html = $html +'<td >'+data[i].teacher+'</td>';
                $html = $html +'<td >'+data[i].classroom+'</td>';
                $html = $html +'<td >'+data[i].lessonTime+'</td>';
                $html = $html +'<td >'+data[i].remark+'</td>';
                $html = $html +'<td><input type="button" class="form-control" value="删除" id="delete" onclick="deleteEvent(this)"/>';
                $html = $html + '</tr>';
                $('tbody').append($html);
             }
        }
    });
}


//添加执行事件
$("#add").click(function(){
    var postData = {
        semester:$("#semester").val(),
        week:$("#week").val(),
        recordDate:$("#recordDate").val(),
        stuNumber:$("#stuNumber").val(),
        stuName:$("#stuName").val(),
        stuClass:$("#stuClass").val(),
        classType:$("#classType").val(),
        date:$("#date").val(),
        lesson:$("#lesson").val(),
        teacher:$("#teacher").val(),
        classroom:$("#classroom").val(),
        lessonTime:$("#lessonTime").val(),
        remark:$("#remark").val()
    }

    var c=confirm("是否添加数据？");
    if( c == true){
        $.ajax({
            type:'POST',
            url:restAPI_url+'absenteeism',
            contentType:'application/json',
            data:JSON.stringify(postData),
            success:function(data){
                showTable();
                emptyInputField();
                alert("添加成功");
                console.log(data);
            }
        })
    }
})

//删除执行事件
function deleteEvent(obj){
    var id=$(obj).parent().parent().children('td:eq(0)').text();
    var c=confirm("确定要删除当前数据吗？");
    if (c==true){
        $.ajax({
            type: 'DELETE',
            url: restAPI_url+'absenteeism/'+id,
            success:function(data){
                showTable();
                emptyInputField();
                alert("数据删除成功");
            }
        });
    }
}